﻿package com.wandian.view.mediator
{
  
    import com.wandian.controller.CLZNoticeName;
    import com.wandian.data.GameData;
    
    import flash.display.MovieClip;
    import flash.events.TimerEvent;
    import flash.utils.ByteArray;
    import flash.utils.Timer;
    
    import org.puremvc.as3.interfaces.*;
    import org.puremvc.as3.patterns.mediator.*;

    public class BroadcastNoticeMediator extends Mediator
    {
        private var _global:GameData;
        private var _timer:Timer;
        private var _sumNum:int;
        private var _curNum:int;
        public static const NAME:String = "BroadcastNoticeMediator";

        public function BroadcastNoticeMediator(param1:Object = null) : void
        {
            this._global = GameData.getInstance();
            this._timer = new Timer(20);
            super(NAME, param1);
            this._timer.addEventListener(TimerEvent.TIMER, this.timerHandler);
            this.mcPanel.mc.mask = this.mcPanel.mask_mc;
            this.mcPanel.visible = false;
            this.checkNotice();
            return;
        }

        override public function onRegister() : void
        {
            return;
        }

        override public function listNotificationInterests() : Array
        {
            return [CLZNoticeName.GAME_NOTICE, CLZNoticeName.OUT_GAME_ROOM_REMOVE];
        }

        override public function handleNotification(param1:INotification) : void
        {
            var _loc_2:Object = null;
            var _loc_3:uint = 0;
            var _loc_4:String = null;
            var _loc_5:String = null;
            switch(param1.getName())
            {
                case CLZNoticeName.GAME_NOTICE:
                {
                    _loc_2 = param1.getBody();
                    if (_loc_2.type == 2)
                    {
                        GameData.arrNoticeMsg.unshift(_loc_2);
                        GameData.arrNoticeMsg.unshift(_loc_2);
                        if (!this._timer.running)
                        {
                            _loc_2 = GameData.arrNoticeMsg.shift();
                            _loc_3 = _loc_2.userid;
                            _loc_4 = _loc_2.name;
                            _loc_5 = _loc_2.msg;
                            this.scrollNotice(_loc_3, _loc_4, _loc_5);
                        }
                    }
                    this.sendNotification(CLZNoticeName.CHAT_RES, _loc_2);
                    break;
                }
                case CLZNoticeName.OUT_GAME_ROOM_REMOVE:
                {
                    this._timer.reset();
                    this._timer.removeEventListener(TimerEvent.TIMER, this.timerHandler);
                    this._timer = null;
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }

        private function scrollNotice(userid:uint, _name:String, msg:String) : void
        {
            this._timer.reset();
            var htmlMsg:String = "";
            if (userid == 0)
            {
                htmlMsg = "<font color=\'#ff0000\'>" + GameData.tip["SYSTEM_NAME"] + ": " + msg + "</font>";
            }
            else
            {
                htmlMsg = "<font color=\'#ff0000\'>" + _name + ": " + msg + "</font>";
            }
            var msgBytes:* = new ByteArray();
            msgBytes.writeMultiByte(htmlMsg, "CN-GB");
            this.mcPanel.mc.width = msgBytes.length * (14 / 2);
            this.mcPanel.mc.x = this.mcPanel.mask_mc.x + this.mcPanel.mask_mc.width;
            this.mcPanel.mc.txt.htmlText = htmlMsg.toString();
            this._timer.start();
            this.mcPanel.visible = true;
            return;
        }

        private function checkNotice() : void
        {
            var msgData:Object = null;
            if (GameData.arrNoticeMsg.length > 0)
            {
                msgData = GameData.arrNoticeMsg.shift();
            }
            else
            {
                return;
            }
            this.scrollNotice(msgData.userid, msgData.name, msgData.msg);
            return;
        }

        private function timerHandler(event:TimerEvent) : void
        {
            if (this.mcPanel.mc.x + this.mcPanel.mc.width <= this.mcPanel.mask_mc.x)
            {
                this.mcPanel.mc.x = this.mcPanel.mask_mc.x + this.mcPanel.mask_mc.width;
                this._timer.reset();
                this.mcPanel.mc.txt.text = "";
                if (GameData.arrNoticeMsg.length > 0)
                {
                    this.checkNotice();
                }
                else
                {
                    this.mcPanel.visible = false;
                }
            }
            (this.mcPanel.mc.x - 1);
            return;
        }

        private function get mcPanel() : MovieClip
        {
            return viewComponent as MovieClip;
        }

    }
}
